# -*- coding: iso-8859-2 -*-

import os
import re
import string    
import datetime
    
from authorshipanalyzer.message import Message
from authorshipanalyzer.message.parser import ParserEarly 
from authorshipanalyzer.message.parser import ParserCurrent     
from authorshipanalyzer.file import Path
    
class MessageReader:
    '''
    Main class for message retrieval. If parses
    selected messages and transform them to the
    form which allow processing.
    '''
    
    def __init__(self, path = None):
        '''
        @param path - path to the files with form messages
        default: resources/info1
        '''
        if path == None:
            self.path = Path.getInfo1Dir()
        else:
            self.path = path
    
    def readAllMessages(self):
        '''
        Function reading all messages form files
        which are stored in a initialized directory
        @raise IOError - if path is not valid
        @return messages
        '''
        
        fromDate = datetime.date(1996, 1, 1)
        toDate = datetime.date.today()    
        
        return self.readMessages(fromDate, toDate)
    
    def readMessages(self, fromDate, toDate):
        '''
        Function reading messages form files 
        which are stored in a initialized directory
        between a given peroid of time
        @param fromDate - start date
        @param toDate - end date
        @raise IOError - if path is not valid
        @return messages
        '''
        parserEarly = ParserEarly()  
        parserCurrent = ParserCurrent()     
        
        if not os.path.exists(self.path) or not os.path.isdir(self.path):
            raise IOError
        
        for file in os.listdir(self.path):
            matchObject = re.match("info1\.(\d+)-(\d+)", file)
            if  matchObject != None:
                
                #year, month, day
                fileDate = datetime.date(string.atoi(matchObject.group(1)), \
                                         string.atoi(matchObject.group(2)), \
                                         1)
                                
                if fileDate > toDate or fileDate < fromDate:
                    continue;
                
                filepath = os.path.join(self.path, file)
                
                if not os.path.exists(filepath):
                    raise IOError
                
                if fileDate < datetime.date(2000, 10, 1):
                        parserEarly.retrieveMessages(filepath)
                else:
                    parserCurrent.retrieveMessages(filepath)
         
        messages = []
        messages.extend(parserEarly.getMessageList())    
        messages.extend(parserCurrent.getMessageList())        
        return messages
    
if __name__ == "__main__":
    from authorshipanalyzer.file import Path
    from time import time
    
    fromDate = datetime.date(2003, 1, 1)
    toDate = datetime.date(2003, 1, 3)
    
    start = time()
    mr = MessageReader(Path.getInfo1Dir())
    messages = mr.readMessages(fromDate, toDate)
    end = time()
    print "Number of parsed messages: " + str(len(messages))
    print "Parse time: " + str(end - start) + " seconds"
    
#    for m in messages:
#        m.printInfo(len(m.getText()))
#        print "\n\n>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>\n\n"

